package com.gator.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import javax.servlet.http.HttpServletRequest;

public class MyZuulFilter extends ZuulFilter {


    //定义过滤器类型
    /*
    public static final String ERROR_TYPE = "error";
    public static final String POST_TYPE = "post";
    public static final String PRE_TYPE = "pre";
    public static final String ROUTE_TYPE = "route";
    *   */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    //执行顺序，数值越小越先执行
    @Override
    public int filterOrder() {
        return 0;
    }


    //是否执行  TRUE FALSE
    @Override
    public boolean shouldFilter() {
        return true;
    }

    //具体操作
    @Override
    public Object run() throws ZuulException {
        System.out.println("Entered MyZuulFilter run Method");

        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();

        String method = request.getMethod();
        String url = request.getRequestURL().toString();
        //也可以获得一些自定义请求参数，如网关鉴权，不过最好将鉴权业务分离出来如使用Security
        //String token = request.getParameter("token");

        System.out.println("method: " + method + ", url: " + url);

        return null;
    }
}
